package com.ruoyi.web.controller.student;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TPupilInfo;
import com.ruoyi.system.service.ITPupilInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学生信息Controller
 *
 * @author xiyu
 * @date 2024-12-01
 */
@RestController
@RequestMapping("/student/pupil")
public class TPupilInfoController extends BaseController {
    @Autowired
    private ITPupilInfoService tPupilInfoService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('student:pupil:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPupilInfo tPupilInfo) {
        startPage();
        List<TPupilInfo> list = tPupilInfoService.selectTPupilInfoList(tPupilInfo);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('student:pupil:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TPupilInfo tPupilInfo) {
        List<TPupilInfo> list = tPupilInfoService.selectTPupilInfoList(tPupilInfo);
        ExcelUtil<TPupilInfo> util = new ExcelUtil<TPupilInfo>(TPupilInfo.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:pupil:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tPupilInfoService.selectTPupilInfoById(id));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('student:pupil:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPupilInfo tPupilInfo) {
        tPupilInfo.setCreateBy(getUsername());
        return toAjax(tPupilInfoService.insertTPupilInfo(tPupilInfo));
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('student:pupil:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPupilInfo tPupilInfo) {
        tPupilInfo.setUpdateBy(getUsername());
        return toAjax(tPupilInfoService.updateTPupilInfo(tPupilInfo));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('student:pupil:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids, @PathVariable String pupilStatus) {
        return toAjax(tPupilInfoService.deleteTPupilInfoByIds(ids, pupilStatus));
    }

    /**
     * 获取有效的学生列表
     */
    @GetMapping("/getValidPupilInfoList")
    public AjaxResult getValidPupilInfoList(TPupilInfo tPupilInfo) {
        if (StringUtils.isEmpty(tPupilInfo.getPupilStatus())) {
            tPupilInfo.setPupilStatus("0");
        }
        return success(tPupilInfoService.getPupilNoOrNameList(tPupilInfo));
    }
}
